<?php

namespace Management\Controller;

use Think\Controller;

use Myself\FileSystem;

class MemberController extends BaseController {



	protected $m = NULL;

	protected $m_class = NULL;

	protected $m_config = NULL;

    protected $statusArr=array("关闭","开放");
    protected $order_statusArr=array(

	    ['color'=>'dark','status'=>"未付款"],
        ['color'=>'default','status'=>"待分配"],
        ['color'=>'default','status'=>"已分配"],
        ['color'=>'blue','status'=>"司机到达"],
        ['color'=>'green','status'=>"待评价"],
        ['color'=>'dark','status'=>"评价完成"],
        ['color'=>'red','status'=>"已退款"],
        ['color'=>'default','status'=>"已取消"],

	);
	

	public function _initialize(){

		parent::_initialize();

		$this->m = M('member');

		

	}

		

	

	

    public function index(){

		header('Content-Type:text/html;charset=utf-8');

		$where=[];

		//-----------------搜索-------------------

		I('key') ? $where['nickname'] = array('like','%'.I('key').'%') : false;
		I('key') ? $where['mobile'] = array('like','%'.I('key').'%') : false;
		$where['_logic'] = 'or';
		if(!empty(I('from'))){
			if(I('from')==1){
				//wx
				$where['from']=array('eq','');
			}else{
				$where['from']=array('eq','baidu');
			}
			$this->assign('from',I('from'));
		}

		//-------------------------搜索end-----------------

		$pageno = I('p',1,'int');

		$pageCount = 10;

		$list = $this->m->where($where)->page($pageno.','.$pageCount)->select();

		$this->assign('list',$list);// 赋值数据集

		$count = $this->m->where($where)->count();// 查询满足要求的总记录数

		$this->assign('count',$count);// 赋值分页输出	

		$Page = new \Think\Page($count,$pageCount);// 实例化分页类 传入总记录数和每页显示的记录数

		$show = $Page->showManage();// 分页显示输出

		$this->assign('page',$show);// 赋值分页输出

        //-------------------------固定参数输出-----------------

        $this->assign('statusArr',$this->statusArr);// 状态

		$this->display('index');

    }

	public function edit(){

		

		

		$id = I('id',0,'int');

		$dataInfo = $this->m->find($id);

		$this->assign('dataInfo',$dataInfo);



		$this->assign('back_url',$_SERVER['HTTP_REFERER']);



		$this->display('edit');

    }

	public function save(){

		

		$temp = $this->m->create();



        $pic_new=I('post.pic_path_new');

        if($pic_new){

            $temp['pic_path']=$this->pic_move($pic_new,$temp['pic_path']);

        }

		if($temp['id']){

			

			$this->m->save($temp);

			$this->selectPage_ajax(array('act' => '修改', 'back_url' => I('back_url'),'jump_url' => U('Cartype/edit','id='.$temp['id'])));

		}else{

		    $result = $this->m->add($temp);

			if($result)

			    $this->selectPage_ajax( array('act' => '添加', 'back_url' => I('back_url'),'jump_url' => U('Cartype/edit','area_id='.I('post.area_id'))));



			else $this->error($GLOBALS['notice']['error'][0]);

		}

	

	}

	public function del(){



		

		$ids = I('id');



		if($ids){

			if(is_array($ids)){$ids=implode(",",$ids);}

			$this->baseDelFile('cartype',$ids,'pic_path');

			if ($this->m->delete($ids)){

				$ids=explode(",",$ids);

			    $this->success($GLOBALS['notice']['success'][1]);

			}else{

				$this->error($GLOBALS['notice']['error'][1]);

			}

		}else{

			$this->error($GLOBALS['notice']['warming'][1]);

		}		

    }

	public function batch(){          ////批量操作方法



		

		$ids = I('id');

		$val = I('get.val');

		$field = I('get.field');

		

		

		

		if($ids){

			if(is_array($ids)){$ids=implode(",",$ids);}

			

			if ($this->m->where('id in ('.$ids.')')->save(array($field=>$val))){

				$ids=explode(",",$ids);

			    $this->success($GLOBALS['notice']['success'][2]);

			}else{

				$this->error($GLOBALS['notice']['error'][2]);

			}

		}else{

			$this->error($GLOBALS['notice']['warming'][1]);

		}		

    }

	public function ajaxFindNext(){



		$selValue = I('selValue','0','int');

		$areaNextId = I('areaNextId','','int');

		$domId = I('domId');

		$classList = $selValue?$this->m_class->where('parent_id='.$selValue)->select():array();

		$content = "";

		if(!count($classList)){

			echo 0;exit;

		}else{

		

			$content .= '<span id="'.$domId.'_'.$areaNextId.'">'."\n\t";

			$content .= '<select class="form-control input-small" name="class_id[]" onchange="javascript:findNext(this.value , '.$areaNextId.' , \''.$domId.'\')">'."\n\t";

			$content .= '<option value="0">请选择</option>'."\n\t";

			foreach($classList as $item){

			$selectedMark = $selValue==$item['id']?'selected':'';

			$content .= '<option value="'.$item['id'].'" '.$selectedMark.'>'.$item['class_name'].'</option>'."\n\t";

			}

			$content .= '</select>'."\n\t";

			$content .= '</span>'."\n\t";

		

			echo $content;exit;

		

		}

	}

	public function ajaxListInput(){

		header('Content-Type:text/html;charset=utf-8');

		$info = I('post.');

		$data[$info["fieldName"]] = $info["Val"];

		$data["id"] = $info["id"];

		if ($this->m->save($data)){

			echo "success";

		}else{

			echo "error";

		}

		exit;

	}

	
	//weapon用户相关订单
    public function order(){ 
		header('Content-Type:text/html;charset=utf-8');
		$where['member_id'] =I('get.id');
		//-----------------搜索-------------------
		if(I('product_id')){
			$where['product_id']=I('product_id');
			$this->assign('product_id',I('product_id'));
		}
        $key = I('key','');//关键字 订单号
		if(!empty($key)){
			$where['order_no'] = array('LIKE','%'.$key.'%');
		}
		$from_time = I('from_time','');
		$to_time = I('to_time','');
		if($from_time){
			$from_time_s = strtotime($from_time);
			$where['addtime']= array('EGT',$from_time_s);
		}
		if($to_time){
			$to_time_s = strtotime($to_time)+86400;
			$where['addtime']= array('ELT',$to_time_s);
		}
		if($from_time && $to_time){
			$where['addtime']=array(array('EGT',$from_time_s),array('ELT',$to_time_s),'and');
		}
		//-------------------------搜索end-----------------
		$pageno = I('p',1,'int');
		$pageCount = 10;
		$list = M('order')->where($where)->page($pageno.','.$pageCount)->order('addtime desc')->select();
		$memberIdList=array_column($list,'member_id');
		$memberList=M('member')->where([
		    'id'=>['in',$memberIdList]
        ])->field('id,nickname,username,avatar')->select();
        $memberList=array_column($memberList,null,'id');
        $this->assign('memberList',$memberList);
        //--------------------订单详情-------------------------------
        foreach ($list as $k=>$v){
            $list[$k]['detail']=M('order_detail')->where(['order_no'=>$v['order_no']])->field('pic_path,title_cn')->select();
        }
		$this->assign('list',$list);// 赋值数据集
		$count = M('order')->where($where)->count();// 查询满足要求的总记录数
		$this->assign('count',$count);// 赋值分页输出	
		$Page = new \Think\Page($count,$pageCount);// 实例化分页类 传入总记录数和每页显示的记录数
		$show = $Page->showManage();// 分页显示输出
		$this->assign('page',$show);// 赋值分页输出
        //-------------------------固定参数输出-----------------
        $this->assign('statusArr',$this->order_statusArr);// 状态
		$this->display('order');
    }	
    //导出用户相关信息
	public function member_excel(){
		$data = M('member')->field(['id','nickname','mobile'])->where('1=1')->select();
        if(empty($data)){
            $this->error("暂无有效记录");
            exit;
        }

        // ll($data);die;
        //导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
        import("Org.Util.PHPExcel");
        import("Org.Util.PHPExcel.Writer.Excel5");
        import("Org.Util.PHPExcel.IOFactory.php");
        $fileName="用户信息表";
        $headArr=array("编号","昵称","手机号");
        $this->getExcel($fileName,$headArr,$data);
    }

    // 导出表格方法
    private function getExcel($fileName,$headArr,$data){
        //对数据进行检验
        if(empty($data) || !is_array($data)){
            die("data must be a array");
        }
        //检查文件名
        if(empty($fileName)){
            exit;
        }
        $date = date("Y_m_d",time());
        $fileName .= "_{$date}.xls";
        //创建PHPExcel对象，注意，不能少了\
        vendor("PHPExcel.Classes.PHPExcel");                              // 导入PHPExcel类库
        $objPHPExcel = new \PHPExcel();
        $objProps = $objPHPExcel->getProperties();

        //设置表头
        $key = ord("A");
        foreach($headArr as $v){
            $colum = chr($key);
            $objPHPExcel->setActiveSheetIndex(0) ->setCellValue($colum.'1', $v);
            $key += 1;
        }
        $column = 2;
        $objActSheet = $objPHPExcel->getActiveSheet();
        foreach($data as $key => $rows){ //行写入
            $span = ord("A");
            foreach($rows as $keyName=>$value){// 列写入
                $j = chr($span);
                $objActSheet->setCellValue($j.$column, $value);
                $span++;
            }
            $column++;
        }
        $fileName = iconv("utf-8", "gb2312", $fileName);
        //重命名表
        // $objPHPExcel->getActiveSheet()->setTitle('test');
        //设置活动单指数到第一个表,所以Excel打开这是第一个表
        $objPHPExcel->setActiveSheetIndex(0);
        ob_end_clean();
        ob_start();
        header('Content-Type: application/vnd.ms-excel;charset=utf-8');
        header("Content-Disposition: attachment;filename=\"$fileName\"");
        header('Cache-Control: max-age=0');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output'); //文件通过浏览器下载
        exit;
    }

	

	

}